package com.jy.task.fileoper;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import com.jy.common.utils.SpringWebContextUtil;
import com.jy.entity.filemanager.TSTaske;
import com.jy.service.filemanager.taskmain.TaskMainService;

/* 案例 同步和不同步的区别  非同步需要加@DisallowConcurrentExecution */
public class CheckFileTask implements Job {

	private static final Logger LOG = LoggerFactory
			.getLogger(CheckFileTask.class);
	private static Timestamp newTimestamp;
	private static TSTaske taskTask;

	@Override
	public void execute(JobExecutionContext context) {
		ApplicationContext ac = SpringWebContextUtil.getApplicationContext();

		TaskMainService serviceTask = (TaskMainService) ac
				.getBean("mainTaskService");
		newTimestamp = new Timestamp(System.currentTimeMillis());
		List<TSTaske> listTask = serviceTask.findTaskRun();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		try {
			// 判断任务是否超过时间 加入超过时间则停止任务
			for (TSTaske obj : listTask) {
				// 现在时间大于有效时间
				if (compareToDate(simpleDateFormat.parse(simpleDateFormat
						.format(new Date())), obj.getEffEndDate())) {
					// 删除task
					serviceTask.deleteNoUserTask(obj);
				}
			}
		} catch (Exception e) {
		}
	}

	private boolean compareToDate(Date date1, Date date2) {
		if (date1.getTime() > date2.getTime()) {
			return true;
		}
		return false;
	}
}
